 /**
 * Description: 物资单位
 * @author:Hunter
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-04-08   Hunter       1.0.0     Initial
 */
package com.metro.paperless.sys.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.paperless.sys.mapper.ISysMaterialsUnitMapper;
import com.metro.paperless.sys.service.inf.ISysMaterialsUnitService;
import com.metro.paperless.sys.beans.vo.SysMaterialsType;
import com.metro.paperless.sys.beans.vo.SysMaterialsUnit;

 /**
 * 物资单位<br>
 *
 * @author:Hunter
 * @since:1.0.0
 * @version [1.0.0, 2018-04-08]
 */
@Service("sysmaterialsunitService")
public class SysMaterialsUnitService extends BaseServiceSupport<SysMaterialsUnit> implements ISysMaterialsUnitService{
    //物资单位Mapper
    @Autowired
    private ISysMaterialsUnitMapper sysmaterialsunitMapper;

    @Override
    public IMapper getMapper() {
        return sysmaterialsunitMapper;
    }

    @Override
    public IResult save(SysMaterialsUnit entity) {
        try {
            sysmaterialsunitMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增物资单位失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(SysMaterialsUnit entity) {
        try {
            sysmaterialsunitMapper.updateNotEmpty(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改物资单位失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            sysmaterialsunitMapper.delete(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            sysmaterialsunitMapper.deleteByIds(ids);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
    
    /**
     * 判断单位是否已存在
     */
    public IResult checkUnit(SysMaterialsUnit entity) {
    	//判断是否已存在
    	List<SysMaterialsUnit> list = sysmaterialsunitMapper.queryForList();
    	if(list != null && list.size()>0 ) {
    		for(int i=0;i<list.size();i++){
        		if(list.get(i).getUnitName().equals(entity.getUnitName()) && list.get(i).getDataStatus() == 1 ) {
        			return ResultCode.DUPLICATE_KEY;
        		}
        	}
    	}
    	return ResultCode.SUCCESS;
    }
    /**
     * 新增单位
     */
    @Override
    public IResult saveUnit(SysMaterialsUnit entity) {
    	
    	//编号自增
    	SysMaterialsUnit entity1 = sysmaterialsunitMapper.queryTopList();
    	if(entity1 != null) {
    		int num = Integer.parseInt(entity1.getUnitCode());
    		num = num +1;
    		entity.setUnitCode(String.valueOf(num));
    	}else {
    		entity.setUnitCode("1");
    	}
    	
        try {
            sysmaterialsunitMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增物资单位失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
    		
    		
}